package com.software.software.controler;

import com.software.software.model.blogInfo;
import com.software.software.model.server.searchMap;
import com.software.software.model.userInfo;
import com.software.software.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    private UserMapper userMapper;
    private searchMap map = new searchMap();
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }
    @GetMapping("/signUp")
    public String signUpPage() {
        return "register";
    }
//  主页面
    @GetMapping(value={"/","/main"})
    public String mainPage(HttpSession session,Model model) {
        return "publicPage";
    }

    @GetMapping("/admin")
    public String adminPage(HttpSession session,Model model) {
        if (session.getAttribute("userInfo") != null && ((userInfo)session.getAttribute("userInfo")).userLevel > 0) return "admin";
        return "404";
    }
//    点击blog链接进入blogShow页面
    @GetMapping("blog/{blogId}")
    public String blogShow(@PathVariable("blogId") int  blogId, HttpServletResponse rep) {
        blogInfo blogInfo = userMapper.searchBlog(String.valueOf(blogId));
        if (blogInfo == null) return "404";
        Cookie cookie = new Cookie("blogId",String.valueOf(blogId));
        rep.addCookie(cookie);
        return "blogShow";
    }

//    搜索栏点击搜索跳转select页面
    @GetMapping("/search")
    public String search(@RequestParam Map<String,String> param,HttpServletResponse rep){
        Cookie cookie = new Cookie("searchContent",param.get("content"));
        rep.addCookie(cookie);
        Cookie cookie2 = new Cookie("searchTag","");
        rep.addCookie(cookie2);
        return "select";
    }

    @GetMapping("/tags/{tagId}")
    public String searchByTag(@PathVariable("tagId") int tagId,HttpServletResponse rep){
        System.out.println(tagId);
        Cookie cookie = new Cookie("searchTag",String.valueOf(tagId));
        rep.addCookie(cookie);
        Cookie cookie2 = new Cookie("searchContent","");
        rep.addCookie(cookie2);
        return "select";
    }
//  写文章页面
    @GetMapping("/write")
    public String writeBlog() {
        return "write";
    }

    @GetMapping("/write/{blogId}")
    public String upDataBlog(@PathVariable("blogId") int  blogId,HttpServletResponse rep,HttpSession session) {
        System.out.println("write/blogId start");
        System.out.println(blogId);
        blogInfo blogInfo = userMapper.searchBlog(String.valueOf(blogId));
        blogInfo.show();
        if (blogInfo == null) {
            return "404";
        }
        Cookie cookie = new Cookie("upDataBlogId",String.valueOf(blogId));
        cookie.setPath("/demo/write/"+blogId);
        rep.addCookie(cookie);
        return "write";
    }

    @GetMapping("/userInfoPage")
    public String homePage(){
        return "userInfoPage";
    }

    @GetMapping("/home/{masterId}")
    public String userPage(@PathVariable("masterId") int  masterId,HttpServletResponse rep,HttpSession session) {
        if(userMapper.searchUserInfo(map.get_map(String.valueOf(masterId),null,null,null)) == null) {
            return "404";
        }
        Cookie cookie = new Cookie("masterId",String.valueOf(masterId));
        cookie.setPath("/demo/home/"+masterId);
        Map<String,String> m = new HashMap<String,String>();
        m.put("userId",String.valueOf(masterId));
        userInfo masterInfo = userMapper.searchUserInfo(m);
        session.setAttribute("masterInfo",masterInfo);
        rep.addCookie(cookie);
        return "home";
    }

    @GetMapping("/faceAdd")
    public String faceAdd(){return "faceAdd";}
    @GetMapping("/faceCheck")
    public String faceCheck(){return "faceCheck";}
}
